package com.hzye.framework.common.util;

import java.security.NoSuchAlgorithmException;


/***
 * @author EX-HUANGBIN001
 * @2011-8-10
 */
public final class StringUtil {

	/** 防止为空时抛异常 **/
	public static String trim( Object o ) {
		return o == null ? "" : o.toString().trim().replaceAll("　", "");
	}

	// 转换成大写
	public static String toUpperCase( Object obj ) {
		return trim(obj).toUpperCase();
	}

	// 转换成小写
	public static String toLowerCase( Object obj ) {
		return trim(obj).toLowerCase();
	}

	/** 比较两个对象的值是否相等，用来比较字符串 不区分大小写 **/
	public static boolean isEqualIgnoreCase( Object o1 , Object o2 ) {
		return trim(o1).equalsIgnoreCase(trim(o2)) ? true : false;
	}

	/** 比较两个对象的值是否不相等，用来比较字符串 不区分大小写 **/
	public static boolean isNotEqualIgnoreCase( Object o1 , Object o2 ) {
		return !trim(o1).equalsIgnoreCase(trim(o2)) ? true : false;
	}

	// 判断对象是否为空
	public static boolean isEmpty( Object obj ) {
		return trim(obj).length() == 0 ? true : false;
	}

	// 判断对象是否不为空
	public static boolean isNotEmpty( Object obj ) {
		return trim(obj).length() == 0 ? false : true;
	}

	// 判断对象字符串是否相等
	public static boolean isEquals( Object obj1 , Object obj2 ) {
		return StringUtil.trim(obj1).equals(StringUtil.trim(obj2));
	}

	/***
	 * 生成指定位数的随机数
	 * 
	 * @param bits
	 * @return
	 */
	public static String random( int bits ) {
		Double d = new Double(Math.random() * Math.pow(10, bits));
		int str = d.intValue();
		return lpad(str + "", bits, "0");
	}

	/***
	 * 截取字符串
	 * 
	 * @param str
	 * @param index
	 *            　指定截取的位数
	 * @return String Jun 9, 2012 1:25:38 PM
	 */
	public static String subString( String str , int index ) {
		String strA = trim(str);
		if (strA.length() > index) {
			return strA.substring(0, index);
		}
		else {
			return str;
		}
	}

	public static String subHtmlString( String content , int index ) {
		return content;
	}

	/***
	 * 解密字符串
	 * 
	 * @param obj
	 * @return String 2012-2-17 下午09:56:52
	 * @throws NoSuchAlgorithmException 
	 */
	public static String getDesString( Object obj ) throws NoSuchAlgorithmException {
		DesUtil des = new DesUtil();// 实例化一个对像
		String str = des.getDesString(StringUtil.trim(obj));
		return str;
	}

	/***
	 * 解密字符串
	 * 
	 * @param obj
	 * @return String 2012-2-17 下午09:56:52
	 * @throws NoSuchAlgorithmException 
	 */
	public static int getDesInteger( Object obj ) throws NoSuchAlgorithmException {
		DesUtil des = new DesUtil();// 实例化一个对像
		return Integer.parseInt(des.getDesString(StringUtil.trim(obj)));
	}

	/***
	 * 加密字符串
	 * 
	 * @param obj
	 * @return String 2012-2-17 下午09:56:31
	 * @throws NoSuchAlgorithmException 
	 */
	public static String getEncString( Object obj ) throws NoSuchAlgorithmException {
		DesUtil dd = new DesUtil();
		String str = dd.getEncString(StringUtil.trim(obj));
		return str;
	}

	public static boolean hasLength( CharSequence str ) {
		return (str != null && str.length() > 0);
	}

	/**
	 * Check that the given String is neither <code>null</code> nor of length 0. Note: Will return <code>true</code> for a String
	 * that purely consists of whitespace.
	 * 
	 * @param str
	 *            the String to check (may be <code>null</code>)
	 * @return <code>true</code> if the String is not null and has length
	 * @see #hasLength(CharSequence)
	 */
	public static boolean hasLength( String str ) {
		return hasLength((CharSequence) str);
	}

	public static String lpad( String str , int num , String replaceStr ) {
		while (str.length() < num) {
			str = replaceStr + str;
		}
		return str;
	}

	public static String rpad( String str , int num , String replaceStr ) {
		while (str.length() < num) {
			str = str + replaceStr;
		}
		return str;
	}
}
